// accounting/account_summary/account_summary.js
let listBehavior = require('../../behavior/list');

let nowYear = new Date().getFullYear(),
  nowMonth = new Date().getMonth() + 1;
nowMonth = nowMonth > 9 ? nowMonth : '0'+nowMonth;
let defPeriod = nowYear + '' + nowMonth;

Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    account_period: defPeriod,

    list: [],

    quickFlag: false,
    searchList: [
      {field_id: ['account_period_s','account_period_e'],field_name: '会计期',dfltVal: [defPeriod,defPeriod],type: 'reference1', optionList: [],optionId: 'account_period',optionName: 'account_period'},
      {field_id: ['account_id_s','account_id_e'],field_name: '科目选择',type: 'reference1', optionList: [],optionId: 'account_id',optionName: 'account_name'},
      {field_id: ['account_level_s','account_level_e'],field_name: '科目级别',type: 'num'},
      {field_id: ['voucher_no_s','voucher_no_e'],field_name: '凭证号',type: 'text'},
    ],
    keyList: [],
    searchObj: {
      account_period_s: defPeriod,
      account_period_e: defPeriod,
      account_id_s: '',
      account_id_e: '',
      account_level_s: 0,
      account_level_e: 0,
      voucher_no_s: 0,
      voucher_no_e: 0,
    },

    activeNav: 'all'

  },

  //切换导航栏
  toggleNav(e){
    let tid = e.currentTarget.dataset.tid;
    this.setData({activeNav: tid});
  },
  navTo(e){
    let {url} = e.currentTarget.dataset;
    wx.navigateTo({
      url
    })
  },
  //显示搜索
  showQuick(){
    this.setData({
      quickFlag: !this.data.quickFlag,
    })
  },
  //搜索
  searchData({detail}){
    let {searchObj} = this.data;

    //快速查询
    wx.$each(detail,(i,item)=>{
      let fields = item.CompareWith.split(',');
      wx.$each(fields,(ind,ite)=>{
        let val = item.CompareValue[ind];
        if (['account_level_s','account_level_e','voucher_no_s','voucher_no_e'].includes(ite) && !val) val = 0;
        searchObj[ite] = val;
      });
    });

    this.setData({searchObj});
    this.getFirstPage();
  },
  getDataList(){
    let {searchObj} = this.data;
    wx.$post(3,'Account/query_all_account_summary_total',searchObj).then(res=>{
      if (res.errcode != 0)return;
      let list = res.data.row || [];
      this.setData({list,page: 1,total: list.length,limit: list.length});
    })
  },
  //获取下拉数据
  getOptionsList (arg){
    let [key,v,url,post] = arg;
    return new Promise((resolve,reject) => {
      wx.$post(v, url, post).then(res=>{
        if (res.errcode != 0){
          reject();
          return;
        }
        let data = res.data.row || [],
          list = data;
        resolve({[key]: list});
      });
    });
  },
  ready(){

    let parmaArr = [
        ['account_period',3,'Account/query_all_account_period',{"limit":0,"page":1}],  //会计期
        ['account_id',3,'Account/query_account',{"page":1,"limit":0,"oby":"account_id"}],  //科目
      ],
      pmArr = [];
    wx.$each(parmaArr,(i,item)=>{
      pmArr.push(this.getOptionsList(item));
    });
    Promise.all(pmArr.map(p => p.catch(err => ''))).then(res=>{
      let resObj = {};
      wx.$each(res,(i,item)=>{
        resObj = {...resObj,...item}
      });
      let searchList = this.data.searchList;
      searchList[0].optionList = resObj.account_period;
      searchList[1].optionList = resObj.account_id;

      this.setData({searchList})
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();
    wx.$getAuth('all_account_summary_total').then(res=>{
      if (!res.tableSee) {
        wx.$toast('抱歉，您没有权限查看该模块数据！',1000).then(()=>{
          wx.navigateBack({ delta:1 });
        });
        return
      }
      this.getDataList();
      this.ready();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
